package com.example.exp_3;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Calculator extends AppCompatActivity {
    int sign = 0;
    Boolean isClicked = false;
    Double first;
    Double second;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);
        TextView display = findViewById(R.id.display);
        Button bt1 = findViewById(R.id.one);
        Button bt2 = findViewById(R.id.two);
        Button bt3 = findViewById(R.id.three);
        Button bt4 = findViewById(R.id.four);
        Button bt5 = findViewById(R.id.five);
        Button bt6 = findViewById(R.id.six);
        Button bt7 = findViewById(R.id.seven);
        Button bt8 = findViewById(R.id.eight);
        Button bt9 = findViewById(R.id.nine);
        Button bt0 = findViewById(R.id.zero);
        Button btback = findViewById(R.id.backspace);
        Button btclean = findViewById(R.id.clean);
        Button btadd = findViewById(R.id.add);
        Button btminus = findViewById(R.id.minus);
        Button btmulti = findViewById(R.id.multi);
        Button btdivide = findViewById(R.id.divide);
        Button btequal = findViewById(R.id.equal);
        btclean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                display.setText("");
                isClicked = false;
                sign = 0;
                first = 0.0;
                second = 0.0;
            }
        });
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt1.getText());
                    } else {
                        display.setText(num+bt1.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt1.getText());
                    isClicked = false;
                }
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt2.getText());
                    } else {
                        display.setText(num+bt2.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt2.getText());
                    isClicked = false;
                }
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt3.getText());
                    } else {
                        display.setText(num+bt3.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt3.getText());
                    isClicked = false;
                }
            }
        });
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt4.getText());
                    } else {
                        display.setText(num+bt4.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt4.getText());
                    isClicked = false;
                }
            }
        });
        bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt5.getText());
                    } else {
                        display.setText(num+bt5.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt5.getText());
                    isClicked = false;
                }
            }
        });
        bt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt6.getText());
                    } else {
                        display.setText(num+bt6.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt6.getText());
                    isClicked = false;
                }
            }
        });
        bt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt7.getText());
                    } else {
                        display.setText(num+bt7.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt7.getText());
                    isClicked = false;
                }
            }
        });
        bt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt8.getText());
                    } else {
                        display.setText(num+bt8.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt8.getText());
                    isClicked = false;
                }
            }
        });
        bt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt9.getText());
                    } else {
                        display.setText(num+bt9.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt9.getText());
                    isClicked = false;
                }
            }
        });
        bt0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClicked) {
                    String num = (String) display.getText();
                    if (num.equals("")) {
                        display.setText(bt0.getText());
                    } else {
                        display.setText(num+bt0.getText());
                    }
                } else {
                    display.setText("");
                    display.setText(bt0.getText());
                    isClicked = false;
                }
            }
        });
        btadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isClicked) {
                    isClicked = true;
                    first = Double.parseDouble(display.getText().toString());
                    sign = 1;
                } else {
                    sign = 1;
                }
            }
        });
        btminus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isClicked) {
                    isClicked = true;
                    first = Double.parseDouble(display.getText().toString());
                    sign = 2;
                } else {
                    sign = 2;
                }
            }
        });
        btmulti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isClicked) {
                    isClicked = true;
                    first = Double.parseDouble(display.getText().toString());
                    sign = 3;
                } else {
                    sign = 3;
                }
            }
        });
        btdivide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isClicked) {
                    isClicked = true;
                    first = Double.parseDouble(display.getText().toString());
                    sign = 4;
                } else {
                    sign = 4;
                }
            }
        });
        btequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                second = Double.parseDouble(display.getText().toString());
                display.setText("");
                double result = 0;
                if (second == 0 && sign == 4) {
                    display.setText("除数不能为0");
                } else {
                    switch (sign) {
                        case 1:
                            result = first + second;
                            break;
                        case 2:
                            result = first - second;
                            break;
                        case 3:
                            result = first * second;
                            break;
                        case 4:
                            result = first / second;
                            break;
                    }
                    display.setText(String.valueOf(result));
                }
            }
        });
        btback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = (String) display.getText();
                if (num == "") {
                    display.setText("");
                } else {
                    num = num.substring(0,num.length()-1);
                    display.setText(num);
                }
            }
        });
    }
}
